import requests
import execjs
import time


# def get_ip():
#     # 提取代理API接口，获取1个代理IP
#     api_url = "https://dps.kdlapi.com/api/getdps/?secret_id=o6ykpp1tqja9zj9g4947&signature=ne52s9ngdvy61nrghkzf7kytjk7c3qgk&num=1&sep=1"
#     # 获取API接口返回的代理IP
#     proxy_ip = requests.get(api_url).text
#
#     username = "d2769292027"
#     password = "8bnrqo2g"
#     proxies = {
#         "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
#         "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
#     }
#     return proxies
#
#
# proxies = get_ip()

appkey = "34839810"
token = "6319069f3d5412071104c3e622375460"

with open("咸鱼.js", "r", encoding="utf-8") as f:
    js_code = execjs.compile(f.read())

headers = {
    "referer": "https://www.goofish.com/",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"
}
cookies = {
    "cna": "yqj3IAd8dz4CAXug4+SxgtkI",
    "_m_h5_tk": f"{token}_1752228399008",
    "_m_h5_tk_enc": "64acbeb6702263539074151cf4a5852b",
    "tfstk": "g8vIppD82y4Iob68V6maC-kNiBBWPck4VusJm3eU29BLyzKA7e7FL0D5yebwLw-FpaeWz3JezLtuFgKJrDbrxf-HxTX-uqWS3HxhAtVZ3p_LBlKGckI-_6UqN9ltuqkqQXEOF2g2zTrAzCslWMIRJwn1BiSfegQL9c_OViV8JUL-fOIhVgFd98KTWg_OyTBJyNQOSg6RvUB-IgqfVX_HADSpPAYzlk9P58eJCG3lkHa_hMJdAzbXYNgYudwcOZKd583dfYVRPNvx7ftMuBLFjUMj5tdD5L1AFvacACKfBwWxdkbvLgt2NngbzGApRdCWQ0cHfL_CMpC_D8WOIgLRNC0b3M6e1s9Bt049vE7BM9xzc2-1GC1cX1E799R20Lfvpvac7sjW5sRIlJTA41yVliuuNl10FG_qfcNuZq-vZROir3gA9GjC_ci_JQfdjG_XUcNHM6IGAZnsfyd1."
}
url = "https://h5api.m.goofish.com/h5/mtop.taobao.idlemtopsearch.pc.search/1.0/"
params = {
    "jsv": "2.7.2",
    "appKey": appkey,
    "t": "1752219283795",
    "sign": "4ebedcab9f42ad173fad8235c4f55ee8",
    "v": "1.0",
    "type": "originaljson",
    "accountSite": "xianyu",
    "dataType": "json",
    "timeout": "20000",
    "api": "mtop.taobao.idlemtopsearch.pc.search",
    "sessionOption": "AutoLoginOnly",
    "spm_cnt": "a21ybx.search.0.0",
    "spm_pre": "a21ybx.home.searchInput.0"
}
for page in range(1, 51):
    data = {
        "data": "{\"pageNumber\":" + str(
            page) + ",\"keyword\":\"手机\",\"fromFilter\":false,\"rowsPerPage\":30,\"sortValue\":\"\",\"sortField\":\"\",\"customDistance\":\"\",\"gps\":\"\",\"propValueStr\":{},\"customGps\":\"\",\"searchReqFromPage\":\"pcSearch\",\"extraFilterValue\":\"{}\",\"userPositionJson\":\"{}\"}"
    }
    t = int(time.time() * 1000)
    sign = js_code.call("get_sign", token, t, appkey, data.get("data"))
    params["t"] = t
    params["sign"] = sign

    response = requests.post(url, headers=headers, cookies=cookies, params=params, data=data)
    print("状态码：", response.status_code, "响应内容：", response.content.decode())

    for item in response.json()['data']['resultList']:
        title = item["data"]["item"]["main"]["exContent"].get("title")
        area = item["data"]["item"]["main"]["exContent"].get("area")
        userNickName = item["data"]["item"]["main"]["exContent"].get("userNickName", "")
        price = item["data"]["item"]["main"]["exContent"].get("price")
        try:
            if price: price = price[1]["text"]
        except Exception as e:
            print(e)
            price = 0
        print(f"页码：{page}, 地区：{area},用户昵称：{userNickName},价格：{price},标题：《{title}》")
